package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.alibaba.fastjson.JSON;
import com.xhwl.centiotdata.emq.EmqClient;
import com.xhwl.centiotdata.emq.MqttProperties;
import com.xhwl.centiotdata.emq.QosEnum;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.impl.AccountServiceImpl;
import com.xhwl.centiotdata.service.impl.DeviceServiceImpl;
import com.xhwl.common.dto.auth.PedestrianAuthDTO;
import com.xhwl.common.dto.cent.pedes.UserDTO;
import com.xhwl.common.dto.cent.pedes.YunpingAuthDTO;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.pojo.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 云屏设备权限下发
 * @author: pengxiao
 * @create: 2022-05-09 09:24
 **/

@Service
@Slf4j
public class PedestrianIntegrationYunPingHandler extends PedestrianIntegrationHandler {

    @Autowired
    private EmqClient emqClient;

    @Autowired
    private DeviceServiceImpl deviceService;

    @Autowired
    private MqttProperties mqttProperties;

    @Autowired
    private AccountServiceImpl accountService;

    @Autowired
    private IAuthAccountCentService authAccountCentService;



    @Override
    public void afterPropertiesSet() throws Exception {
        PedestrianIntegrationFactory.register(7, this);
    }

    @Override
    public ResultJson createAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        // 查询人员
        List<AccountDTO> accountDTOList = accountService.queryAccount(pedestrianAuthDTO.getProjectId(), Arrays.asList(pedestrianAuthDTO.getSourceId()));
        UserDTO userDTO = new UserDTO();
        AccountDTO accountDTO = accountDTOList.get(0);
        BeanUtils.copyProperties(accountDTO, userDTO);
        // 新增人员(包括人脸)
        authAccountCentService.save(configDTO.getProjectId(), accountDTO.getId(),String.valueOf(accountDTO.getId()),null,configDTO.getProductId());

        // 授权
        YunpingAuthDTO authDTO = new YunpingAuthDTO();
        authDTO.setUserId(pedestrianAuthDTO.getSourceId());
        authDTO.setFaceUrl(pedestrianAuthDTO.getFaceUrl());
        authDTO.setFeature(pedestrianAuthDTO.getFeature());
        authDTO.setCardStatus(pedestrianAuthDTO.getCardStatus());
        authDTO.setCardNumber(pedestrianAuthDTO.getCardNumber());
        authDTO.setCardNumberList(pedestrianAuthDTO.getCardNumberList());
        authDTO.setDeviceOpenIdList(pedestrianAuthDTO.getThirdDeviceIdList());
        authDTO.setStartTime(pedestrianAuthDTO.getValidStartTime());
        authDTO.setEndTime(pedestrianAuthDTO.getValidEndTime());
        authDTO.setSynType(pedestrianAuthDTO.getSynType());
        authDTO.setAddedDeviceOpenIdList(pedestrianAuthDTO.getAddedThirdDeviceIdList());
        authDTO.setDeletedDeviceOpenIdList(pedestrianAuthDTO.getDeletedThirdDeviceIdList());
        authDTO.setPhone(pedestrianAuthDTO.getPhone());
        authDTO.setAccountName(pedestrianAuthDTO.getAccountName());
        log.info("给员工{}下发云屏设备授权：{}", pedestrianAuthDTO.getSourceId(), authDTO.toString());
        sendToEmp(mqttProperties.getAuthCreateTopic(), String.valueOf(pedestrianAuthDTO.getProjectId()), authDTO);

        try {
            onSavingAuthCards(authAccountCentService, configDTO, pedestrianAuthDTO);
        } catch (Exception e) {
            // 保存转换卡号数据失败
        }
        return ResultJson.success();
    }

    @Override
    public ResultJson updateAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        // 更新、新增共用接口
        createAuth(configDTO, pedestrianAuthDTO);
        return ResultJson.success();
    }

    @Override
    public ResultJson deleteAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {

        // 授权
        YunpingAuthDTO authDTO = new YunpingAuthDTO();
        authDTO.setUserId(pedestrianAuthDTO.getSourceId());
        authDTO.setSynType(pedestrianAuthDTO.getSynType());
        sendToEmp(mqttProperties.getAuthCreateTopic(), String.valueOf(pedestrianAuthDTO.getProjectId()), authDTO);

        return ResultJson.success();
    }

    /**
     * 消息发送至empx
     * @param noticeTopic
     * @param projectId
     * @param pwdKey
     */
    private void sendToEmp(String noticeTopic, String projectId, Object pwdKey) {
        // 更新成功时下发至设备端
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("projectId", projectId);
        retMap.put("result", pwdKey);
        emqClient.publish(noticeTopic + "/" + projectId, JSON.toJSONString(retMap), QosEnum.QoS1, false);
    }
}
